/**
 * 版权所有(C)2013-2016 无锡全家网络科技有限公司有限公司  公司名称：无锡全家网络科技有限公司有限公司 公司网址：http://www.quanhouse.com
 * <p>
 * Compiler: JDK1.7
 * <p>
 * 版本: 全家网络全民销售平台2.0
 * <p>
 * 作者: qingsong_mei  创建日期: 2013年10月7日  上午10:44:14 
 * <p>
 * 负责人: qingsong_mei
 * <p>
 * 修改者：    修改日期：
 * <p>
 */

package com.work.cos.service.impl.pay;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.work.cos.domain.entity.pay.ComRefundEntity;
import com.work.cos.domain.entity.pay.ComTradeEntity;
import com.work.cos.domain.vo.pay.FConditionVo;
import com.work.cos.service.interfc.pay.IComTradeService;
import com.work.cos.util.page.Page;


/**
 *类功能说明：
 */
@Service
public class ComTradeServiceImpl implements IComTradeService{
	
	@Autowired
    IComTradeMapper tradeRepository;
	
	@Autowired
    ComTradeMapper tradeRepositoryJPA;
	
	@Autowired
    IComRefundMapper refundRepository;
	
	/*@Autowired
	MemberEntityRepositoryJPA			memberEntityRepositoryJPA;*/
	
	@Override
	public List<ComTradeEntity> listAll()
	{
		return tradeRepository.findAll();
	}

	@Override
	@Transactional
	public ComTradeEntity saveTrade(ComTradeEntity trade)
	{
		return tradeRepository.save(trade);
	}

	@Override
	@Transactional(readOnly = true)
	public ComTradeEntity findTradeByID(Long id)
	{
		return tradeRepository.findOne(id);
	}

	@Override
	@Transactional(readOnly = true)
	public ComTradeEntity findByOutTradeNo(String outTradeNo)
	{
		return tradeRepository.findByOutTradeNo(outTradeNo);
	}

	@Override
	@Transactional(readOnly = true)
	public ComTradeEntity findByTradeNo(String tradeNo)
	{
		return tradeRepository.findByTradeNo(tradeNo);
	}
	
	@Override
	@Transactional
	public ComRefundEntity saveRefund(ComRefundEntity refund)
	{
		return refundRepository.saveAndFlush(refund);
	}

	@Override
	@Transactional(readOnly = true)
	public ComRefundEntity findRefundByID(Long id)
	{
		return refundRepository.findOne(id);
	}

	@Override
	@Transactional(readOnly = true)
	public ComRefundEntity findRefundByBatchNo(String batchNo)
	{
		return refundRepository.findByBatchNo(batchNo);
	}
	
	
	
	@Override
	public Page<ComTradeEntity> listRefundTrades(List<String> refunded, Pageable pageable)
	{
		Page<ComTradeEntity> page = tradeRepository.findByRefundedInOrderByRefundedAscRefundEnableTimeAsc(refunded, pageable);
		List<ComTradeEntity> trades = page.getContent();
		Map<Long, Object> map = new HashMap<Long, Object>();
		for (ComTradeEntity trade : trades)
		{
			map.put(Long.valueOf(trade.getOwner()), trade.getOwner());
		}
		Long[] uidKeys = map.keySet().toArray(new Long[] {});
		// 获取用户id对应姓名
		List<Object> idNames = new  ArrayList<Object>();
		if(uidKeys != null && uidKeys.length > 0)
		{
			//idNames = memberEntityRepositoryJPA.listIdAndNameByIds(uidKeys);
		}
		for (Object idName : idNames)
		{
			Object[] o = (Object[]) idName;
			map.put(Long.parseLong(String.valueOf(o[0])), String.valueOf(o[1]));
		}
		for (ComTradeEntity trade : trades)
		{
			trade.setOwner(String.valueOf(map.get(Long.valueOf(trade.getOwner()))));
		}
		return new PageImpl<ComTradeEntity>(trades, pageable, page.getTotalElements());
//		return tradeRepository.findByRefundedInOrderByRefundedAscRefundEnableTimeAsc(refunded, pageable);
	}
	
	@Override
	public Page<Object> listTradesByCondition(FConditionVo vo, Pageable pageable)
	{
		return tradeRepositoryJPA.listTasksByCondition(vo, pageable);
	}
	
	@Override
	public List<ComTradeEntity> listByTradeIds(List<Long> tradeIds)
	{
		return tradeRepository.findByIdIn(tradeIds);
	}
}
